package com.ttg.web.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.common.entities.BasePage;
import com.ttg.common.entities.BaseResult;
import com.ttg.common.enums.ErrorCodeEnums;
import com.ttg.common.enums.GradeTypeEnum;
import com.ttg.common.enums.ResultStatusEnum;
import com.ttg.common.enums.StatusEnum;
import com.ttg.common.exception.BaseException;
import com.ttg.common.utils.UserUtil;
import com.ttg.model.dto.MctInfoListGetDto;
import com.ttg.model.dto.VipInfoListGetDto;
import com.ttg.model.mapper.VipWhiteListMapper;
import com.ttg.model.pojo.BankPublicPrimitive;
import com.ttg.model.pojo.VipInfo;
import com.ttg.model.pojo.VipWhiteList;
import com.ttg.model.vo.*;
import com.ttg.web.dao.BankPublicPrimitiveDao;
import com.ttg.web.dao.VipGradeConfigDao;
import com.ttg.web.dao.VipInfoDao;
import com.ttg.web.dao.VipWhiteListDao;
import com.ttg.web.service.VipWhiteListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class VipWhiteListServiceImpl extends ServiceImpl<VipWhiteListMapper, VipWhiteList> implements VipWhiteListService {
    @Autowired
    private VipWhiteListDao vipWhiteListDao;

    @Autowired
    private BankPublicPrimitiveDao bankPublicPrimitiveDao;

    @Autowired
    private VipInfoDao vipInfoDao;

    @Autowired
    private VipGradeConfigDao vipGradeConfigDao;

    @Override
    public BasePage<VipWhiteListMctGetVo> mctGetList(MctInfoListGetDto mctInfoListGetDto) {
        if (Objects.isNull(mctInfoListGetDto)) {
            throw new BaseException(ErrorCodeEnums.PARAM_IS_NULL);
        }
        BasePage<Object> parse = mctInfoListGetDto.parse();
        BasePage<VipWhiteListMctGetVo> page = new BasePage<>(parse.getCurrent(), parse.getSize(), parse.getTotal(), parse.isSearchCount());
        return vipWhiteListDao.mctGetList(page, mctInfoListGetDto);

    }
    @Override
    public BasePage<VipWhiteListVipGetVo> vipGetList(VipInfoListGetDto vipInfoListGetDto) {
        if (Objects.isNull(vipInfoListGetDto)) {
            throw new BaseException(ErrorCodeEnums.PARAM_IS_NULL);
        }
        BasePage<Object> parse = vipInfoListGetDto.parse();
        BasePage<VipWhiteListVipGetVo> page = new BasePage<>(parse.getCurrent(), parse.getSize(), parse.getTotal(), parse.isSearchCount());
        return vipWhiteListDao.vipGetList(page, vipInfoListGetDto);

    }

    @Override
    public BaseResult addOrEdit(VipWhiteListAddOrEditVo vipWhiteListAddOrEditVo) {
        List<VipWhiteListVo> list = vipWhiteListAddOrEditVo.getVipWhiteListVos();
        for (VipWhiteListVo vipWhiteListVo : list) {
            VipWhiteList vipWhiteList = new VipWhiteList();
            vipWhiteList.setVipGrade(vipWhiteListVo.getVipGrade());
            vipWhiteList.setType(vipWhiteListVo.getType());
            vipWhiteList.setContentId(vipWhiteListVo.getContentId());
            if (vipWhiteListDao.list(new LambdaQueryWrapper<VipWhiteList>()
                    .eq(VipWhiteList::getContentId, vipWhiteListVo.getContentId())
                    .eq(VipWhiteList::getType, vipWhiteListVo.getType())).isEmpty()){
                vipWhiteList.setAddTime(LocalDateTime.now());
                vipWhiteList.setAddId(UserUtil.getUserThrow().getAdmId());
            }
            vipWhiteList.setStatus(vipWhiteListVo.getStatus()!=null?vipWhiteListVo.getStatus():0);
            vipWhiteList.setUpdateTime(LocalDateTime.now());
            vipWhiteList.setUpdateId(UserUtil.getUserThrow().getAdmId());
            vipWhiteListDao.saveOrUpdate(vipWhiteList, new LambdaUpdateWrapper<VipWhiteList>()
                    .eq(VipWhiteList::getContentId, vipWhiteListVo.getContentId())
                    .eq(VipWhiteList::getType, vipWhiteListVo.getType()));
            if (StatusEnum.ENABLE.getValue()==vipWhiteList.getStatus()){
                renewGrade(vipWhiteList);
            }else {
                renewGradeByVipId(vipWhiteList);
            }
        }
        return BaseResult.success(ResultStatusEnum.SUCCESS);
    }

    private void renewGrade(VipWhiteList vipWhiteList) {
        if (GradeTypeEnum.TYPE_PRIVATE.getValue() == vipWhiteList.getType()) {
            vipInfoDao.update(new LambdaUpdateWrapper<VipInfo>()
                    .set(VipInfo::getVipGrade, vipWhiteList.getVipGrade())
                    .eq(VipInfo::getVipId, vipWhiteList.getContentId()));
        } else if (GradeTypeEnum.TYPE_PUBLIC.getValue() == vipWhiteList.getType()) {
            bankPublicPrimitiveDao.update(new LambdaUpdateWrapper<BankPublicPrimitive>()
                    .set(BankPublicPrimitive::getMctGrade, vipWhiteList.getVipGrade())
                    .eq(BankPublicPrimitive::getMctNo, vipWhiteList.getContentId()));
        }
    }

    @Override
    public BaseResult updateStatus(VipWhiteListDetailVo vipWhiteListDetailVo) {
        List<VipWhiteListVo> list = vipWhiteListDetailVo.getVipWhiteListVos();
        for (VipWhiteListVo vipWhiteListVo : list) {
            Long id = vipWhiteListVo.getId();
            if (id == null) {
                throw new BaseException(ErrorCodeEnums.PARAM_IS_NULL);
            }
            VipWhiteList byId = vipWhiteListDao.getById(id);
            if (byId == null) {
                throw new BaseException(ErrorCodeEnums.PARAM_IS_NULL);
            }
            vipWhiteListDao.update(new LambdaUpdateWrapper<VipWhiteList>()
                    .set(VipWhiteList::getStatus, vipWhiteListVo.getStatus())
                    .set(VipWhiteList::getUpdateTime, LocalDateTime.now())
                    .set(VipWhiteList::getUpdateId, UserUtil.getUserThrow().getAdmId())
                    .eq(VipWhiteList::getId,vipWhiteListVo.getId()));
            if (StatusEnum.ENABLE.getValue()==vipWhiteListVo.getStatus()){
                renewGrade(byId);
            }else {
                renewGradeByVipId(byId);
            }
        }
        return BaseResult.success(ResultStatusEnum.SUCCESS);
    }

    private void renewGradeByVipId(VipWhiteList vipWhiteList) {
        if (GradeTypeEnum.TYPE_PRIVATE.getValue() == vipWhiteList.getType()) {
            String newGradeByVipId = vipGradeConfigDao.getVipNewGradeByVipId(vipWhiteList.getContentId());
            vipInfoDao.update(new LambdaUpdateWrapper<VipInfo>()
                    .set(VipInfo::getVipGrade, newGradeByVipId)
                    .eq(VipInfo::getVipId, vipWhiteList.getContentId()));
        } else if (GradeTypeEnum.TYPE_PUBLIC.getValue() == vipWhiteList.getType()) {
            String newGradeByVipId = vipGradeConfigDao.getMctNewGradeByVipId(vipWhiteList.getContentId());
            bankPublicPrimitiveDao.update(new LambdaUpdateWrapper<BankPublicPrimitive>()
                    .set(BankPublicPrimitive::getMctGrade, newGradeByVipId)
                    .eq(BankPublicPrimitive::getMctNo, vipWhiteList.getContentId()));
        }
    }
}
